package com.ruitu.health_1.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.health_1.R;
import com.ruitu.health_1.mvp.model.ArticleModel;
import com.ruitu.health_1.mvp.presenter.ArticlePresenter;
import com.ruitu.router_module.bean.article.ArticleInfo;

import java.util.ArrayList;
import java.util.List;

public class ArticleDetailsActivity extends BaseActivity<ArticlePresenter.ArticleDetailsP, ArticleModel.ArticleDetailsM> {
    private WebView web;
    private String id;
    private ProgressBar progress;
    private RecyclerView recyclerview;
    private DataAdapter adapter;
    private LinearLayout ll_recommend;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_article_details;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getIntent().getStringExtra("id");
        p.articleInfo(id);
//        Logg.i("ID: " + id);
        setHeadTitle("文章详情");
    }

    private Drawable drawable;

    @SuppressLint("JavascriptInterface")
    @Override
    protected void initViews() {
        super.initViews();
        web = findViewById(R.id.web);
        progress = findViewById(R.id.progress);
        recyclerview = findViewById(R.id.recyclerview);
//        ll_recommend = findViewById(R.id.ll_recommend);

        WebSettings settings = web.getSettings();
        //如果访问的页面中要与JavascriptEnabled交互,则webview必须设置支持JavaScript
//        settings.setJavaScriptEnabled(true);
        //设置自适应屏幕,两者合用
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

//        web.addJavascriptInterface(new JSInteration(), "android");
        web.setWebViewClient(new WebViewClient());

        web.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    progress.setVisibility(View.GONE);
//                    ll_recommend.setVisibility(View.VISIBLE);
                } else {
                    progress.setVisibility(View.VISIBLE);
                    progress.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        recyclerview.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new DataAdapter(this);
        recyclerview.setAdapter(adapter);
        List<ArticleInfo> articleInfos = new ArrayList<>();
        articleInfos.add(new ArticleInfo());
        articleInfos.add(new ArticleInfo());
        articleInfos.add(new ArticleInfo());
        adapter.setDataList(articleInfos);

    }

    private ArticleInfo articleInfo;

    public void setData(ArticleInfo info) {
        this.articleInfo = info;
        web.loadUrl(info.getArticleUrl());
    }

    @Override
    protected void onResume() {
        super.onResume();
        //激活webview的状态,能正常加载网页
        web.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //清除网页访问留下的缓存
        //由于内核缓存是全局的因此这个方法不仅仅针对webview而是针对整个应用程序.
        web.clearCache(true);

        //清除当前webview访问的历史记录
        //只会webview访问历史记录里的所有记录除了当前访问记录
        web.clearHistory();

        //这个api仅仅清除自动完成填充的表单数据，并不会清除WebView存储到本地的数据
        web.clearFormData();
        web.destroy();
    }

    class DataAdapter extends ListBaseAdapter<ArticleInfo> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_recommend_product;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_recommend_product = holder.getView(R.id.iv_recommend_product);
            ImageView iv_add_shopping_cart = holder.getView(R.id.iv_add_shopping_cart);
            TextView tv_recommend_product_name = holder.getView(R.id.tv_recommend_product_name);
            TextView tv_recommend_product_info = holder.getView(R.id.tv_recommend_product_info);
            TextView tv_price = holder.getView(R.id.tv_price);
        }
    }
}
